package com.example.passwordmanager;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.Ohuang.ilivedata.LiveDataBus;
import com.Ohuang.ilivedata.MyLiveData;
import com.example.passwordmanager.databinding.FragmentRechangePsdBinding;
import com.example.util.base_util.SPUtil;


public class ChangePsdFragment extends Fragment {
    FragmentRechangePsdBinding fragmentRechangePsdBinding;
    private View root;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root=inflater.inflate(R.layout.fragment_rechange_psd, container, false);
        fragmentRechangePsdBinding=FragmentRechangePsdBinding.bind(root);
        initView();
        return root;
    }

    private void initView(){
        fragmentRechangePsdBinding.imageClose.setOnClickListener(v->{
            getActivity().finish();
        });

        fragmentRechangePsdBinding.imageNext.setOnClickListener(v->{
            fragmentRechangePsdBinding.input0.setErrorEnabled(false);
            fragmentRechangePsdBinding.input.setErrorEnabled(false);
            fragmentRechangePsdBinding.input2.setErrorEnabled(false);

            String oldpsd=fragmentRechangePsdBinding.etOldPassword.getText().toString();
            String psd=fragmentRechangePsdBinding.etPassword.getText().toString();
            String psd2=fragmentRechangePsdBinding.etPassword2.getText().toString();

            String s= (String) SPUtil.get(getContext(),"password","");
            if (TextUtils.isEmpty(oldpsd)){
                fragmentRechangePsdBinding.input0.setError("密码不能为空");
                return;
            }
            if (TextUtils.isEmpty(psd)){
                fragmentRechangePsdBinding.input.setError("密码不能为空");
                return;
            }
            if (TextUtils.isEmpty(psd2)){
                fragmentRechangePsdBinding.input2.setError("密码不能为空");
                return;
            }
            if (!TextUtils.equals(s,oldpsd)){
                fragmentRechangePsdBinding.input0.setError("密码错误");
                return;
            }
            if (!TextUtils.equals(psd,psd2)){
                fragmentRechangePsdBinding.input2.setError("两次密码输入不一致");
                return;
            }

            SPUtil.put(getContext(),"password",psd);

            MyLiveData.getInstance().with("startFg",Fragment.class).setValue(new LoginFragment());

        });
    }

}